## Table H.2: Firm and Employee Donations and Lobbying of Specific Politicians, 
# First-Time Donation in 3 Years, Services Firms, Full Results: ----------------

## Instructions ----------------------------------------------------------------
# Step 1: Adjust MAIN_DIR to main folder
# Step 2: Adjust DATA_DIR to where data is is located
# Step 3: Adjust FIG_DIR to where output folder is
# Step 4: Run entire script
## setup -----------------------------------------------------------------------

# clean slate
rm(list = ls())

# load packages
library(dplyr)
library(stargazer)
library(lfe)
library(tidyr)
options(scipen=999)

# set directory
MAIN_DIR <- "C:/Users/js.egb/Dropbox/campaign-lobby-paper/replication_package"

# set data folder
DATA_DIR <-  paste(MAIN_DIR, "/data/", sep = "")

# set output folder
FIG_DIR <- paste(MAIN_DIR, "/output/", sep = "")


# set industries to be analyzed. Services.
naics_sub <- c("42", "44", "45", "48", "49", 
               "51", "52", "53", "54", "55", "56",
               "61", "62", "71", "72", "81") 
# 42	Wholesale Trade
# 44-45	Retail Trade
# 48-49	Transportation and Warehousing
# 51	Information
# 52	Finance and Insurance
# 53	Real Estate and Rental and Leasing
# 54	Professional, Scientific, and Technical Services
# 55	Management of Companies and Enterprises <- none in publicly traded
# 56	Administrative and Support and Waste Management and Remediation Services
# 61	Educational Services
# 62	Health Care and Social Assistance
# 71	Arts, Entertainment, and Recreation
# 72	Accommodation and Food Services
# 81	Other Services (except Public Administration)


# file to be read in
#file_in <- paste0(DATA_DIR, "don_lob_all_ind_20210203.csv") # file to be input
file_in <- paste0(DATA_DIR, "don_lob_all_ind_20210203.rds") # file to be input


# read data
don_lob <- readRDS(file_in) %>%
  filter(naics2 %in% naics_sub) %>%
  mutate(l_sale = log(sale+1), 
         l_emp = log(emp+1), 
         l_don_tot = log(don_tot+1)) %>%
  select(gvkey_govtrack_id, gvkey, govtrack_id, year, naics, naics3, naics2, chamber, 
         lob_any_bin, lob_spon_bin, lob_cosp_bin, lob_comm_bin,
         don_any_bin, treat_don_all,
         don_tot, l_don_tot,
         l_sale, l_emp, # firm covariates
         majority, democrat, com_chair, power_com) %>% # recodings
  mutate(gvkey_govtrack_id_numeric = as.integer(as.factor(gvkey_govtrack_id)),
         year = as.integer(year),
         l_treat_don_all = ifelse(treat_don_all == 1, l_don_tot, 0)) 

gc()
gc()


# create treatment indicators, first donation within 3-years
don_lob <- don_lob %>%
  arrange(gvkey, govtrack_id, year) %>%
  group_by(gvkey, govtrack_id) %>%
  mutate(treat_don_all_3y = ifelse(don_tot > 0 & lag(don_tot, 1) == 0 & lag(don_tot, 2) == 0 & lag(don_tot, 3) == 0, 1, 0)) %>%
  mutate(l_treat_don_all_3y = ifelse(treat_don_all_3y == 1, l_don_tot, 0)) %>%
  replace_na(list(treat_don_all_3y = 0, l_treat_don_all_3y = 0))
gc()


# Analysis with both Chambers, 3 year first-time Donation at time t: binary donation
lm1 <- lfe::felm(lob_any_bin ~ treat_don_all_3y + majority + democrat + com_chair + power_com | gvkey | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
lm2 <- lfe::felm(lob_any_bin ~ treat_don_all_3y + majority + democrat + com_chair + power_com | gvkey + year | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
lm3 <- lfe::felm(lob_any_bin ~ treat_don_all_3y + majority + democrat + com_chair + power_com | gvkey + year + govtrack_id | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
lm4 <- lfe::felm(lob_any_bin ~ treat_don_all_3y + majority + democrat + com_chair + power_com | gvkey_govtrack_id + year | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
gc()



# Analysis with both Chambers,  3 year first-time Donation at time t: logged donation amount
lm5 <- lfe::felm(lob_any_bin ~ l_treat_don_all_3y + majority + democrat + com_chair + power_com | gvkey | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
lm6 <- lfe::felm(lob_any_bin ~ l_treat_don_all_3y + majority + democrat + com_chair + power_com | gvkey + year | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
lm7 <- lfe::felm(lob_any_bin ~ l_treat_don_all_3y + majority + democrat + com_chair + power_com | gvkey + year + govtrack_id | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
lm8 <- lfe::felm(lob_any_bin ~ l_treat_don_all_3y + majority + democrat + com_chair + power_com | gvkey_govtrack_id + year | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
gc()


stargazer::stargazer(lm1, lm2, lm3, lm4, lm5, lm6, lm7, lm8, type = "latex", 
                     title = "\\textbf{Firm and Employee Donations and Lobbying of Specific Politicians, First-Time Donation in 3 Years, Services Industries, Full Results:} The level of analysis is the firm-politician-year, and the data include all publicly traded services firms (NAICS 2-digit codes 42-81) and members of Congress between 2008 and 2018. The dependent variable is a dummy which equals 1 if firm $i$ lobbied the legislative activity of a politician $j$ in year $t$. $Donationt$ equals 1 if firm $i$ donated to a politician $j$ for the first time in 3 years $t$, and 0 otherwise, whereas $Donation Amount_{t}$ is the logged amount of the 3-year first-time donation. The coefficients come from ordinary least squares regressions, using combinations of firm, politician, year, and firm-politician fixed effects. All models control for politician partisanship, majority status, committee chairmanship, and being member of the top 4 powerful committees. The standard errors are clustered by firm.", 
                     label = "tab_h2",
                     out = paste0(FIG_DIR, "table_h2.tex"),
                     covariate.labels = c("$Donation_{t}$","$Donation\\;Amount_{t}$",
                                          "$Majority_{t}$", "$Democrat_{t}$", 
                                          "$Committee\\;Chair_{t}$", "$Powerful\\;Committee_{t}$"), 
                     dep.var.labels = c("Lobbying Legislative Activity$_{t}$"),
                     add.lines = list(
                       #c("Politician Controls",  rep(c("$\\checkmark$", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"), 2)),
                       c("Firm FEs",  rep(c("$\\checkmark$", "$\\checkmark$", "$\\checkmark$", ""), 2)),
                       c("Year FEs",  rep(c("", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"), 2)),
                       c("Politician FEs",rep(c("", "", "$\\checkmark$", ""), 2)),
                       c("Firm-Politician FEs",rep(c("", "", "", "$\\checkmark$"), 2))),
                     omit.stat = c("rsq", "ser"),
                     # keep = c("treat_don_all", "l_treat_don_all"),
                     column.sep.width = "3pt", notes.align = "l")

# substantive interpretation of donation amount on lobbying probability
log(1.1)*coef(lm8)["l_treat_don_all_3y"] # 10% increase in donations is 0.00031 increase in prob, i.e 0.03pp

rm(lm1, lm2, lm3, lm4, lm5, lm6, lm7, lm8)
gc()
